package com.wiloon.java8x.thread;

import java.util.concurrent.TimeUnit;

public class DaemonThread {
    public static void main(String[] args) {
        Thread t1 = new MyCommon();
        Thread t2 = new Thread(new MyDaemon());
        t2.setDaemon(true); // set as daemon thread

        t2.start();
        t1.start();
    }

    static class MyCommon extends Thread {
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println("common thread:" + i);
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class MyDaemon implements Runnable {
        public void run() {
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                System.out.println("daemon thread:" + i);
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

